package walletadminlogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePayChannelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatePayChannelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePayChannelLogic {
	return &UpdatePayChannelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改支付通道
func (l *UpdatePayChannelLogic) UpdatePayChannel(in *pb.UpdatePayChannelReq) (*pb.UpdatePayChannelResp, error) {
	defer utils.RecoverPanic()

	var pc *wallet.PayChannelConfig
	var err error
	pc, err = l.svcCtx.PayChannelConfigModel.FindOne(l.ctx, in.PayChannelId)
	if err != nil && err != wallet.ErrNotFound {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	if pc == nil {
		l.Logger.Errorf("PayChannelConfig not found !")
		return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, "not found")
	}
	if in.State != 0 {
		pc.State = in.State
	}
	if in.PayChannelName != "" {
		pc.PayChannelName = in.PayChannelName
	}
	//if in.MinAmount != 0 {
	//	pc.MinAmount = in.MinAmount
	//}
	//if in.MaxAmount != 0 {
	//	pc.MaxAmount = in.MaxAmount
	//}
	if in.IsInput != 0 {
		pc.IsInput = in.IsInput
	}
	if in.Logo != "" {
		pc.Logo = in.Logo
	}

	err = l.svcCtx.PayChannelConfigModel.Update(l.ctx, pc)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	return &pb.UpdatePayChannelResp{}, nil
}
